package com.eascs.web.p2p.action;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.common.util.StringUtil;
import com.eascs.p2p.common.entity.BannerSetEntity;
import com.eascs.p2p.common.service.IBannerService;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.Render;
import com.eascs.site.view.ViewContext;
import com.eascs.web.p2p.utils.StringUtils;

@RestController
@RequestMapping("/bannerRest")
public class BannerRestController extends BaseRestController {

	@Reference
	IBannerService bannerService;
	
	@Value("${web.upload.path}")
	private String uploadPath;
	
	@Value("${web.image.domain}")
	private String imageDomain;		//图片服务器域名

	@RequestMapping(value = "/saveBanner")
	public BaseRestResult<String> saveBanner(BannerSetEntity vo) {

		if(StringUtil.isNull(vo.getImageUrl())){
			return BaseRestResult.warn("请上传banner图");
			
		}
		if (StringUtil.isNotBlank(vo.getId())) {

			ModelResult<Boolean> model = bannerService.updateBannerSetEntity(vo);
			if (model.isSuccess()) {
				if (model.getModel()) {

					return BaseRestResult.warn("修改成功").andRedirect("/banner/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("修改失败");
			}
		} else {
			ModelResult<String> savemodel = bannerService.addBannerSetEntity(vo);
			if (savemodel.isSuccess()) {
				if (StringUtil.isNotBlank(savemodel.getModel())) {

					return BaseRestResult.warn("添加成功").andRedirect("/banner/index");
				} else {
					return BaseRestResult.error("数据库操作失败");
				}
			} else {
				return BaseRestResult.info("添加失败失败");
			}
		}
	}

	/**
	 * @Title: isDisplay
	 * @Description:控制前台是否显示
	 * @author 张庆奇
	 * @param id
	 * @param isDisplay
	 * @return
	 * @return BaseRestResult<String>
	 * @throws @date
	 *             2016年7月11日 下午4:55:39
	 */
	@RequestMapping(value = "/isDisplay")
	public BaseRestResult<String> isDisplay(String id, int isDisplay) {
		BaseRestResult<String> result = null;

		ModelResult<Boolean> flag_model = bannerService.updateBannerStatus(id, isDisplay);
		boolean flag = false;
		if (flag_model.isSuccess()) {
			flag = flag_model.getModel();
		}

		if (flag) {
			result = BaseRestResult.warn("操作成功").andRedirect("/banner/index");
		} else {
			result = BaseRestResult.error("操作失败");
		}

		return result;

	}
	
	@RequestMapping(value = "/preview_banner")
	public HtmlRestResult preview_banner(String id) {
		ModelResult<BannerSetEntity> model= bannerService.findBannerSetEntityDetail(id);
		BannerSetEntity vo=new BannerSetEntity();
		if(model.isSuccess()){
			vo=model.getModel();
		}
		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return HtmlRestResult.popHtmlFragment("/banner/banner_preview");
	}

	@RequestMapping(value = "/banner_file/upload/{type}")
	public BaseRestResult<Render[]> fileUpload(@RequestParam(value = "file", required = false) MultipartFile file,
			@PathVariable("type") String type) throws Exception {
		
		String fileName = file.getOriginalFilename();
		String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();// 后缀名
		//新文件名称
		String newfilename = System.currentTimeMillis() + StringUtils.getRandomString(2) + "." + suffix;
		
		String spr = File.separator;								//系统文件分隔符
		//文件相对路径(type加上4位数的年月)
		String relatePath = type + spr + StringUtils.getFourBitYearMonth(new Date());
		String filePath = uploadPath + relatePath+spr;	
		String imageUrl = imageDomain +spr+ relatePath +spr+ newfilename;	//图片url
		String url= imageUrl.replaceAll("\\\\", "/");
		
		FileUtils.writeByteArrayToFile(new File(filePath + newfilename), file.getBytes());

		return BaseRestResult.render(new Render[] { new Render("#imageUrl", url) });
	}

}
